package com.opc.work;

import cn.hutool.core.date.DateUtil;
import com.opc.entity.ConsumeStatistic;
import com.opc.entity.DeviceRecord;
import com.opc.service.ConsumeStatisticService;
import com.opc.service.DeviceRecordService;
import com.opc.service.DeviceService;
import com.opc.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author SEG
 */
@Slf4j
@Component
public class ConsumeStatisticAnalysisSchedule_abandon {
    @Resource
    private ConsumeStatisticService statisticService;

    @Resource
    RedisUtils redisUtils;

    @Resource
    private DeviceService deviceService;

    @Resource
    private DeviceRecordService recordService;

    /**
     * 指定每天7点执行
     */
//    @Scheduled(cron = "0 0 7 * * ?")
    public void consumeReading() {
        Date currentDate = new Date();
        Date startTime = DateUtil.offsetDay(currentDate, -1);
        //. 查找所有产线
        List<String> lineList = null;
        if (redisUtils.exists("pro_line_list")) {
            lineList = Arrays.asList(String.valueOf(redisUtils.get("pro_line_list")).split(","));
        } else {
            lineList = deviceService.selectAllProLine();
            String codesStr = String.join(",", lineList);
            redisUtils.set("pro_line_list", codesStr, 30 * 24 * 60 * 60L);
        }
        //. 根据产线查找设备
        for (String proLine : lineList) {
            List<DeviceRecord> list = recordService.selectConsumeByProLine(startTime, currentDate, proLine);
            //存储数据
            if (list == null || list.size() == 1) {
                continue;

            }
            //
            Map<String, Integer> conMap = new ConcurrentHashMap<>();
            Integer subtraNum = 0;
            for (DeviceRecord record : list) {
                conMap.put(record.getDeviceCode(), record.getVal());
                if (Objects.equals(record.getDeviceCode(), "val_sum")) {
                    continue;
                }
                if (redisUtils.exists(record.getDeviceCode())) {
                    subtraNum += Integer.parseInt(redisUtils.get(record.getDeviceCode()).toString());
                }
                // 根据产线存储数据，更新设备的7点时刻的值  devicecode "val" 0, 设置有效时间 48小时
                redisUtils.set(record.getDeviceCode(), record.getVal(), 48 * 60 * 60L);
            }
            //.求指定产线产线设备的val总和，然后减去所有设备的7点的值的和
            Long consumeVal = (long) (conMap.get("val_sum") - subtraNum);
            ConsumeStatistic consumeStatistic = new ConsumeStatistic();
            consumeStatistic.setStatisticTime(currentDate);
            consumeStatistic.setCreateTime(currentDate);
            consumeStatistic.setProductionLine(proLine);
            consumeStatistic.setType(0);
            consumeStatistic.setConsumeVal(consumeVal);
            boolean flag = statisticService.save(consumeStatistic);
            if (!flag) {
                log.error("save statistic data occur error datetime: {}", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            }
        }
    }

}



